
<script type="text/javascript" src="js/auditor.js"></script>
<style>
    .td{
        line-height: normal;
        word-wrap: break-word;
        word-break: break-all;
        margin-top: 5px;
        /*padding: 8px 0px;*/
        padding: 5px 0px;
    }
    .td p{
        padding: 8px 0px;
    }
</style>

<div class="padding-big">
    <div class="op">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">开始时间：</label>
                <div class="layui-input-inline">
                    <input type="date" name="from" id="from" lay-verify="date" placeholder="yyyy-MM-dd"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline" style="margin-left: 0px">
                <label class="layui-form-label">结束时间：</label>
                <div class="layui-input-inline">
                    <input type="date" name="to" id="to" lay-verify="date" placeholder="yyyy-MM-dd"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">用户名：</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" id="username" maxlength="16" lay-verify="title" autocomplete="off"
                           placeholder="请输入用户名" class="layui-input">
                </div>
            </div>
            <button class="layui-btn layui-btn-normal" id="errorSearch"><i class="fa fa-search"></i>&nbsp;搜索</button>
            <button class="layui-btn layui-btn-normal" id="saveError" onclick="saveErrorLog()"><i class=""></i>&nbsp;点击备份错误日志</button>
            <button class="layui-btn layui-btn-normal" id="eStatisticalBtn"><i class="fa fa-line-chart"></i>&nbsp;点击查看统计信息</button>
        </div>
    </div>
    <div class="offcial-table margin-top clearfix" style="background-color: #fff;">
        <div class="offcial-table tr-border margin-big-top clearfix">
            <div class="tr-th clearfix">
                <div class="th w15">
                   <span style="margin-left: 30px;">
                    错误模块
                    <i class="fa fa-arrow-circle-up fa-gray fa-fw" title="按错误模块升序排列" id="errorModuleAesc" style="cursor: pointer"></i>
                    <i class="fa fa-arrow-circle-down fa-gray fa-fw" title="按错误模块降序排列" id="errorModuleDesc" style="cursor: pointer"></i>
                    </span>
                </div>
                <div class="th w15">
                   <span style="margin-left: 30px;">
                    用户名
                    <i class="fa fa-arrow-circle-up fa-gray fa-fw" title="按用户名升序排列" id="errorUsernameAesc" style="cursor: pointer"></i>
                    <i class="fa fa-arrow-circle-down fa-gray fa-fw" title="按用户名降序排列" id="errorUsernameDesc" style="cursor: pointer"></i>
                    </span>
                </div>
                <div class="th w15">
                    <span style="margin-left: 30px;">
                    操作类型
                        <i class="fa fa-arrow-circle-up fa-gray fa-fw" title="按用户名升序排列" id="typeAesc" style="cursor: pointer"></i>
                    <i class="fa fa-arrow-circle-down fa-gray fa-fw" title="按用户名降序排列" id="typeDesc" style="cursor: pointer"></i>
                    </span>
                </div>
                <div class="th w30">
                    错误详细信息
                </div>
                <div class="th w10">
                    用户IP
                </div>
                <div class="th w15">
                   <span style="margin-left: 30px;">
                    报错时间
                    <i class="fa fa-arrow-circle-up fa-gray fa-fw" title="按报错时间升序排列" id="timeAesc" style="cursor: pointer"></i>
                    <i class="fa fa-arrow-circle-down fa-gray fa-fw" title="按报错时间降序排列" id="timeDesc" style="cursor: pointer"></i>
                    </span>
                </div>
            </div>

            <div id="tbody">
            </div>
        </div>
    </div>

    <div class="show-page padding-big-right" >
        <div class="page">
            <div class="page">
                <p style="font-size: 14px">
                    统计符合条件的信息共:&nbsp;<span id="numValue" style="color: red;">0 </span>&nbsp;条
                </p>
                <ul class="offcial-page margin-top margin-big-right" style="padding-bottom: 15px">
                    <li>
                        <a href="javascript:void(0)" onclick="logsErrorFirstPage()" class="next" >首     页</a>
                    </li>
                    <li>
                        <a href="javascript:void(0)" onclick="logsErrorLastPage()" class="next">上一页</a>
                    </li>
                    <li><span class="fl">第<em class="margin-small-left margin-small-right" id="pageNum">1</em>页</span>
                    </li>
                    <li>
                        <a href="javascript:void(0)" onclick="logsErrorNextPage()" class="next">下一页</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>

</div>

<script>

    //延迟任务查询当前本地错误日志文件是否即将到达上限
    // setTimeout(function(){
    //     $.ajax({
    //         url:HOST.concat("/geterrorlogarrivedmax?token="+config.token+"&timeStamp="+new Date().getTime()),
    //         type:"get",
    //         dataType:"json",
    //         success:function(data){
    //             if (data.meta.state=="success"){
    //                 var res=data.data.isAlert;
    //                 if (res==true||res=="true"){
    //                     alert(data.data.message);
    //                 }
    //             } else{
    //                 console.log("查询失败");
    //             }
    //         },
    //         error:function (data) {
    //             console.log("服务器异常");
    //         }
    //     });
    // },5000);

    /**
     * 错误日志统计
     * */
    $("#eStatisticalBtn").die().live("click",function(){
        $("#contentArea").empty();
        $.get("errorStatisticalInfo.html",function(data){
            $("#contentArea").html(data);
        });
    });

    var num=1;
    var dataInfo={
        "username":"",
        "fromTime":"",
        "toTime":"",
        "errorModuleSort":"",
        "errorUsernameSort":"",
        "errorTimeSort":"",
        "typeSort":"",
        "click":"false"
    };

    /**
     * 根据错误模块
     * */
    $("#errorModuleAesc").live("click",function () {
        dataInfo.errorModuleSort="0";
        dataInfo.errorUsernameSort="";
        dataInfo.errorTimeSort="";
        dataInfo.typeSort="";
        dataInfo.click="false";
        getErrorLogsInfoList(num);
    });
    $("#errorModuleDesc").live("click",function () {
        dataInfo.errorModuleSort="1";
        dataInfo.errorUsernameSort="";
        dataInfo.errorTimeSort="";
        dataInfo.typeSort="";
        dataInfo.click="false";
        getErrorLogsInfoList(num);
    });


    /**
     * 根据错误用户
     * */
    $("#errorUsernameAesc").live("click",function () {
        dataInfo.errorModuleSort="";
        dataInfo.errorUsernameSort="0";
        dataInfo.errorTimeSort="";
        dataInfo.typeSort="";
        dataInfo.click="false";
        getErrorLogsInfoList(num);
    });
    $("#errorUsernameDesc").live("click",function () {
        dataInfo.errorModuleSort="";
        dataInfo.errorUsernameSort="1";
        dataInfo.errorTimeSort="";
        dataInfo.typeSort="";
        dataInfo.click="false";
        getErrorLogsInfoList(num);
    });
    /**
     * 根据时间
     * */
    $("#timeAesc").live("click",function () {
        dataInfo.errorModuleSort="";
        dataInfo.errorUsernameSort="";
        dataInfo.errorTimeSort="0";
        dataInfo.typeSort="";
        dataInfo.click="false";
        getErrorLogsInfoList(num);
    });
    $("#timeDesc").live("click",function () {
        dataInfo.errorModuleSort="";
        dataInfo.errorUsernameSort="";
        dataInfo.errorTimeSort="1";
        dataInfo.typeSort="";
        dataInfo.click="false";
        getErrorLogsInfoList(num);
    });
    /**
     * 根据操作类型
     * */
    $("#typeAesc").live("click",function () {
        dataInfo.errorModuleSort="";
        dataInfo.errorUsernameSort="";
        dataInfo.errorTimeSort="";
        dataInfo.typeSort="0";
        dataInfo.click="false";
        getErrorLogsInfoList(num);
    });
    $("#typeDesc").live("click",function () {
        dataInfo.errorModuleSort="";
        dataInfo.errorUsernameSort="";
        dataInfo.errorTimeSort="";
        dataInfo.typeSort="1";
        dataInfo.click="false";
        getErrorLogsInfoList(num);
    });

    /**
     * 获取指定页码的错误日志
     * */
    function getErrorLogsInfoList(pageNum){
      listErrorLogsByCond(dataInfo.username, dataInfo.fromTime,  dataInfo.toTime, pageNum, pageSize);
    }
    /**
     * 点击备份错误日志
     * */
    function saveErrorLog(){
        isOnline("sysAuditor");
        window.location.href=HOST.concat("/storesLog?username="+getSessionStorage("username") + "&token=" + config.token+"&type=error");
    }

    /**
     * 获取上一页
     * */
    function logsErrorLastPage(){
        if(num<=1){
            num = 1;
            alert("已经是第一页!");
            $("#pageNum").text(num);
        }else{
            num--;
            dataInfo.click="false";
            getErrorLogsInfoList(num);
        }
    }

    /**
     * 获取下一页
     * */
    function logsErrorNextPage(){
        num++;
        dataInfo.click="false";
        getErrorLogsInfoList(num);
    }

    /**
     * 获取首页
     * */
    function logsErrorFirstPage(){
        if(num==1){
            alert("已经是首页");
        }else{
            num = 1;
            $("#pageNum").text(num);
            dataInfo.click="false";
            getErrorLogsInfoList(num);
        }
    }

    /**
     * 查询错误日志记录接口
     * @param pageNum
     * @param pageSize
     * @param callback
     */
    function listErrorLogsByCond(username, from, to, pageNum, pageSize){
        isOnline("sysAuditor");
        $.ajax({
            url: HOST.concat("/listErrorLogInfo?username="+username
                +"&fromTime="+ from
                + "&toTime="+ to
                +"&pageNum=" + pageNum
                + "&pageSize=" + pageSize
                +"&errorModuleSort="+dataInfo.errorModuleSort
                +"&errorUsernameSort="+dataInfo.errorUsernameSort
                +"&errorTimeSort="+dataInfo.errorTimeSort
                +"&typeSort="+dataInfo.typeSort
                    +"&click="+dataInfo.click
                + "&token=" + config.token) ,
            type: "GET",
            success: function(datas){
                checkLogin(datas);
                // console.log(datas)
                num=pageNum;
                addErrorContent(datas);
            },
            error: function(){
                console.log("请求错误日志列表失败！");
            },
            dataType: "json",
        });
    }

    /**
     * 添加错误日志
     * */
    function addErrorContent(res){
        //查询成功
        // console.log(res);
        if(res.meta.state==="success") {
            var list = res.data.resArray;
            // console.log("有查询记录");
            $("#numValue").text(res.data.resCount);
            $("#tbody").empty();
            $("#pageNum").text(num);
            addErrorTd(list);
        }
        //查询失败
        else if(res.meta.state=="failed"){
            if(num==1){
                $("#tbody").empty();
                $("#numValue").text(0);
                //alert("无查询结果!");
            }else {
                //alert(res.meta.message);
                alert("已经是最后一页")
                num--;
            }
        }
    }

    /**
     * 添加具体日志内容
     * */
    function addErrorTd (list){
        for (var p in list) {
            var item = list[p];//单条数据
            var createTime = item["createTime"];
            var operatorType = item["operatorType"];
            var moduleName = item["moduleName"];
            var userName = item["username"];
            var ipAddress = item["ipAddress"];
            var errorRunningInfo = item["errorRunningInfo"];

            errorRunningInfo=errorRunningInfo.toString().trim();

            $("#tbody").append("<div class=\"op-data tr clearfix border-bottom-none\">" +
                "                        <div class=\"td w15\"><p >" + moduleName + "</p></div>" +
                "                        <div class=\"td w15\"><p >" + userName + "</p></div>" +
                "                        <div class=\"td w15\"><p >" + operatorType + "</p></div>" +
                "                        <div class=\"td w30\">" + errorRunningInfo+"</div>" +
                "                        <div class=\"td w10\"><p>" + ipAddress + "</p></div>" +
                "                        <div class=\"td w15\"><p>" + createTime + "</p></div>" +
                " </div>");

        }
    }

    $("#errorSearch").die().live("click",function () {
        dataInfo.username=$("#username").val();
        dataInfo.fromTime=$("#from").val();
        dataInfo.toTime=$("#to").val();
        dataInfo.errorModuleSort="";
        dataInfo.errorUsernameSort="";
        dataInfo.errorTimeSort="";
        dataInfo.click="true";

        if( dataInfo.username.trim()!="" && !checkLogUsername(dataInfo.username)){
            alert("用户名不能超过16位，且不包含除出下划线和减号以外的特殊字符");
            $("#username").val("");
            return;
        }
        if(!isNull(dataInfo.fromTime)&&!isNull( dataInfo.toTime)) {
            if (compareFromAndTo( dataInfo.fromTime, dataInfo.toTime)){
                dataInfo.fromTime = dataInfo.fromTime.replace(/-/g, "/") + " 00:00:00";
                dataInfo.toTime =  dataInfo.toTime.replace(/-/g, "/") + " 23:59:59";
            }else {
                alert("时间区间先后存在问题!");
                return;
            }
        }else if(!isNull(dataInfo.fromTime)){
            dataInfo.fromTime = dataInfo.fromTime.replace(/-/g, "/") + " 00:00:00";
        }else if(!isNull( dataInfo.toTime)){
            dataInfo.toTime =  dataInfo.toTime.replace(/-/g, "/") + " 23:59:59";
        }
        getErrorLogsInfoList(1);
    });

    getErrorLogsInfoList(1)


</script>


